Component({
    options: {
        styleIsolation: 'shared'
    },
    properties: {
        active: {
            type: Number,
            value: 0
        },
        placeholder: {
            type: Boolean,
            value: true
        }
    },
    data: {
        cartCheckNum: 0,
    },
    methods: {
        /**tabbar切换刷新部分数据 */
        tabBarChange(event) {
            const index = event.currentTarget.dataset.index || event.detail
            wx.switchTab({
                url: ['/pages/trading_hall/index', '/pages/supplier/index', '/pages/index/index', '/pages/cart/index', '/pages/my/index'][index],
                success() {
                    const timeId = setTimeout(() => {
                        const pages = getCurrentPages()
                        const currentPage = pages[pages.length - 1]
                        currentPage.refresh()
                        clearTimeout(timeId)
                    }, 100)
                }
            })
        },
        //外部通过获取该组件实例调用该方法修改购物车的数量显示
        setCartCheckNum(num) {
            this.setData({ cartCheckNum: num })
        },
    },
    lifetimes: {
        attached: function() {
            let Authorization = wx.getStorageSync('Authorization')
            if (!Authorization) return
            const cartCheckNum = getApp().globalData.cartCheckNum
            if (cartCheckNum == undefined) {
                const pages = getCurrentPages()
                const currentPage = pages[pages.length - 1]
                currentPage.request({
                    url: '/api/cart/index',
                    hideFailToast: true
                }, res => {
                    this.setData({ cartCheckNum: res.total.count })
                    getApp().globalData.cartCheckNum = res.total.count
                })
            } else {
                this.setData({ cartCheckNum })
            }
        }
    },
    pageLifetimes: {
        show: function() {
            const cartCheckNum = getApp().globalData.cartCheckNum
            this.setData({ cartCheckNum })
        }
    },
})